﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace MusicSearch
{
    class BaiduMusicSearch
    {
        private const string searchUrl = "http://sug.music.baidu.com/info/suggestion?format=json&from=0&callback=&word=";
        private const string infoUrl = "http://play.baidu.com/data/cloud/songlink?hq=0&type=m4a%2Cmp3&rate=&pt=0&flag=-1&s2p=-1&prerate=-1&bwt=-1&dur=-1&bat=-1&bp=-1&pos=-1&auto=-1&songIds=";

        public static string wget(String url)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = WebRequestMethods.Http.Get;
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
            string html = reader.ReadToEnd();
            reader.Close();
            response.Close();
            return html;
        }


        public static List<object> Search(String key)
        {
            string result = wget(searchUrl + Uri.EscapeUriString(key));

            if (result.Contains("errno"))
            {
                //result = wget(searchUrl + Uri.EscapeUriString(title));

                if (result.Contains("errno"))
                {
                    return new List<object>();
                }
            }

            result = result.Substring(1, result.Length - 3);
            JObject json = JObject.Parse(result);
            JArray list = (JArray)json["data"]["song"];
            int count = list.Count;
            List<object> musics = new List<object>();

            for (int i = 0; i < count; i++)
            {
                object obj = list[i];
                //BaiduMusic music = JsonConvert.DeserializeObject<BaiduMusic>(obj.ToString());
                musics.Add(obj);
            }

            return musics;

            //JObject json = JObject.Parse(result);
            //JArray list = (JArray)json["data"]["song"];

            //if (list.Count <= 0)
            //{
            //    return null;
            //}

            //JObject songItem = (JObject)list[0];

            //foreach (JObject item in list)
            //{
            //    if (title.Equals((string)item["songname"]))
            //    {
            //        songItem = item;
            //        break;
            //    }
            //}

            //string id = (string)songItem["songid"];
            //string infoResult = wget(infoUrl + id);
            //JObject infoJson = JObject.Parse(infoResult);
            //JArray infoList = (JArray)infoJson["data"]["songList"];
            //return (string)infoList[0]["lrcLink"];
        }

        public static JObject GetInfo(JObject music)
        {
            string result = wget(infoUrl + music["songid"]);
            JObject json = JObject.Parse(result);
            JObject obj = (JObject)json["data"]["songList"][0];
            return obj;
        }

    }

    class BaiduMusic
    {
        public string songid { get; set; }
        public string songname { get; set; }
        public string artistname { get; set; }
        public string info { get; set; }
        public string bitrate_fee { get; set; }
        public string weight { get; set; }
        public string resource_type { get; set; }
        public string has_mv { get; set; }
        public int copy_type { get; set; }
        public string yyr_artist { get; set; }
        public string resource_type_ext { get; set; }
        public string resource_provider { get; set; }
        public string control { get; set; }
        public string encrypted_songid { get; set; }

    }
}
